﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ProgramMain.Framework
{
    public partial class frmBase : DevExpress.XtraEditors.XtraForm
    {
        public frmBase()
        {
            InitializeComponent();

            m_UniqueIdent = GenerateUniqueIdent(FormName());

            this.Icon = GlobalData.DefaultIcon();
        }

        private Nullable<int> m_Detail_ID;
        /// <summary>
        /// Фильтр по объекту
        /// </summary>
        public Nullable<int> DetailID
        {
            get { return m_Detail_ID; }
            set
            {
                m_Detail_ID = value;
                OnDetailIDChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// Событие, срабатывает на изменение фильтра по объекту
        /// </summary>
        public event EventHandler DetailIDChanged;

        protected virtual void OnDetailIDChanged(EventArgs e)
        {
            if (DetailIDChanged != null)
                DetailIDChanged(this, e);
        }

        private string m_UniqueIdent;
        /// <summary>
        /// Уникальный идентификатор формы
        /// </summary>
        public string UniqueIdent
        {
            get { return m_UniqueIdent; }
            set
            {
                m_UniqueIdent = value;
            }
        }

        /// <summary>
        /// Изменить идентификатор формы
        /// </summary>
        public static string GenerateUniqueIdent(string Class)
        {
            return Class;
        }
        public static string GenerateUniqueIdent(string Class, string strValue)
        {
            return GenerateUniqueIdent(Class) + "_" + strValue;
        }
        public static string GenerateUniqueIdent(string Class, Int64 intValue)
        {
            return GenerateUniqueIdent(Class, intValue.ToString());
        }
        public static string GenerateUniqueIdent(string Class, string strValue, Int64 intValue)
        {
            return GenerateUniqueIdent(Class, strValue + ": " + intValue.ToString());
        }

        public virtual string FormName()
        {
            return GetType().FullName;
        }

        public static frmBase FindForm(string UniqueIdent)
        {
            Form main = Program.MainForm;//Application.OpenForms[0];
            if (main is Form)
            {
                foreach (Form ctl in main.MdiChildren)
                {
                    if (ctl is frmBase)
                    {
                        frmBase bform = (frmBase)ctl;
                        if (bform.UniqueIdent == UniqueIdent)
                            return bform;
                    }
                }
            }
            return null;
        }

        public void ShowAsMDI(Form frmMdiParent)
        {
            this.MdiParent = frmMdiParent;
            this.WindowState = FormWindowState.Maximized;
            this.Show();
        }
    }
}
